package root.ProcessingCapacityScheduling;

import java.util.ArrayList;
import java.util.List;

public class ProcessingCapacityScheduler {
    private List<String> nodes; // 计算节点
    private List<Integer> weights; // 每个节点的处理能力权重
    private int totalWeight; // 权重总和
    private int currentIndex; // 当前轮询位置

    public ProcessingCapacityScheduler() {
        nodes = new ArrayList<>();
        weights = new ArrayList<>();

        // 初始化节点和每个节点的处理能力权重
        nodes.add("Node 1");  // 处理能力权重 1
        nodes.add("Node 2");  // 处理能力权重 3
        nodes.add("Node 3");  // 处理能力权重 2
        weights.add(1);
        weights.add(3);
        weights.add(2);

        totalWeight = weights.stream().mapToInt(Integer::intValue).sum();
        currentIndex = 0;
    }

    // 根据权重分配任务
    public String scheduleTask() {
        String selectedNode = nodes.get(currentIndex);

        // 将当前轮询位置根据权重更新
        currentIndex = (currentIndex + 1) % totalWeight;

        return selectedNode;
    }

    public static void main(String[] args) {
        ProcessingCapacityScheduler scheduler = new ProcessingCapacityScheduler();

        // 模拟10个任务的调度
        for (int i = 0; i < 10; i++) {
            System.out.println("Task " + (i+1) + " assigned to " + scheduler.scheduleTask());
        }
    }
}